#ifndef _DB_INTERFACE_H_
#define _DB_INTERFACE_H_

#include "Include.h"

#include <mysql.h>
#include <my_global.h>

class DBInterface
{
private:
	static MYSQL* Connect();

	static std::string DB_ADDR;
	static std::string DB_USER;
	static std::string DB_PWD;
	static std::string DB_NAME;
	static int DB_PORT;

	static int MAX_CONNECTION;
	static int MIN_CONNECTION;

public:



	static void Init(const std::string& addr , const std::string& user , const std::string& pwd , const std::string& name , const int port , const int max , const int min);

	static std::vector<MYSQL*> vec_db_connection;
	static int db_connection_count;

	static ACE_Thread_Mutex db_lock;
	
public:

	static MYSQL* GetConnection();
	static void ReleaseConnection(MYSQL* conn);
	static void DeleteAllConnections();

	static void Match2StringValue(MYSQL_BIND * bv , char * buf , int size , bool read);
	static void Match2IntValue(MYSQL_BIND * bv , int * pV);
	static void Match2CharValue(MYSQL_BIND * bv , char * pV);
	static void Match2DoubleValue(MYSQL_BIND * bv , double * pV);


	static _DB_RESULT SelectOne(const std::string& sql , MYSQL_BIND * param , MYSQL_BIND * result);
	static _DB_RESULT Execute(const std::string& sql , MYSQL_BIND * param);

	static _DB_RESULT GetControllerTimeElapsed(const std::string& renderer_id , int& primary_elapsed , int& secondary_elapsed);
	static _DB_RESULT SetControllerTime(const std::string& renderer_id  , const bool is_primary);

	static _DB_RESULT AddAgent(const std::string& renderer_id);
	static _DB_RESULT AddAgent(const std::string& renderer_id , const std::string& agent_ip);
	static _DB_RESULT DelAgent(const std::string& renderer_id , const std::string& agent_ip);
	static _DB_RESULT GetAgentStates(const std::string& renderer_id , std::map<std::string , _SOCKET_STATE>& agent_map);
	static _DB_RESULT SetStoppedAgent(const std::string& renderer_id , const std::string& agent_ip);
	static _DB_RESULT SetStoppingAgent(const std::string& renderer_id , const std::string& agent_ip);
	static _DB_RESULT SetStoppingAgents(const std::string& renderer_id , const std::vector<std::string>& vec_ip);
	static _DB_RESULT GetAgentMode(const std::string& renderer_id , const std::string& agent_ip , char& mode);


	static _DB_RESULT GetRegInstanceCount( const std::string& renderer_id , int& count, const bool always = false);
	static _DB_RESULT GetRegInstanceList(const std::string& renderer_id , std::vector<std::string>& vecInstance , const bool always = false);
	static _DB_RESULT GetStoppedAgentInstanceList(const std::string& renderer_id , std::vector<std::string>& vecInstance);
	static _DB_RESULT GetRunningAgentInstanceList(const std::string& renderer_id , std::vector<std::string>& vecInstance);


	static _DB_RESULT ClearProcessingJobs(const std::string& renderer_id , const std::string& agent_ip);
	static _DB_RESULT AddJob(const std::string& renderer_id , const std::string& agent_ip , const std::string& job_id);
	static _DB_RESULT DelJob(const std::string& job_id);
	static _DB_RESULT DelJobList(const std::vector<std::string>& vecJobId);

	static _DB_RESULT ResetMonitorData(const std::string& renderer_id);


#ifdef _TEST_
	static bool test_SetRunningAgent(const std::string& renderer_id , const std::string& instance_id);
	static bool test_SetAllStoppingAgentsToStop(const std::string& renderer_id);
#endif

};



#endif
